const DP = [0, 1, 2, 3, 1, 2, 3];
//             1  2  3  4  5  6

/**
 * @param {number} n
 * @return {number}
 */
var numSquares = function(n) {
  if (n >= DP.length) {
    for (let i = DP.length; i <= n; i++) {
      const arr = [];
      const rt = Math.sqrt(i);
      const scale = Math.floor(rt);
      for (let t = scale; t > 1; t--) {
        arr.push(DP[i - t * t]);
      }
      DP.push(Math.min(...arr) + 1);
    }
  }
  return DP[n];
};

console.log(numSquares(14));
console.log(JSON.stringify(DP))